package com.ihelp.rest.mis.platform.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ihelp.rest.base.hibernate4.BaseHibernateDao;
import com.ihelp.rest.mis.pojo.Site;

@Repository
public class SiteDao  extends BaseHibernateDao<Site, String>{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Map<String,Object>> getAllSiteList(int start, int pageSize,String typecode){
		StringBuilder sb = new StringBuilder();
		sb.append("select * from (");
		sb.append("select a.id,a.typecode,a.url,a.isabnormal,a.addtime,b.typename,rownum rn from SITE a,sitetype b where a.typecode = b.typecode ");
		if(StringUtils.isNotBlank(typecode)&& !typecode.equals("0")){
			sb.append(" and a.typecode like '"+typecode+"%' " );
		}
		sb.append(")where rn between  "+(start-1)*pageSize+" and "+(start*pageSize) );
		return this.jdbcTemplate.queryForList(sb.toString()); 
	}
	
	public int getAllSiteListSize(String typecode){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from (");
		sb.append("select a.id,a.typecode,a.url,a.isabnormal,a.addtime,b.typename,rownum rn from SITE a,sitetype b where a.typecode = b.typecode ");
		if(StringUtils.isNotBlank(typecode)&& !typecode.equals("0")){
			sb.append(" and a.typecode like '"+typecode+"%' " );
		}
		sb.append(")" );
		return this.jdbcTemplate.queryForInt(sb.toString()); 
	}
}
